package cn.com.elite;

import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import com.elite.algorithm.GUID;
import com.elitecrm.saas.service.EliteSAASException;

public class Loadkscomments {
	private static final Log log = LogFactory.getLog(Loadkscomments.class);
	
	public String loadkscomments(DataSource dataSource,String ep_id,Document doc,String HandleGroup_ID,String Staff_ID)throws Exception{
		String ReturnXml="";
		Element ExamMainIDEle=(Element)doc.selectSingleNode("/Root/ExamMainID");
		if(ExamMainIDEle!=null){
			String ExamMainID=ExamMainIDEle.getText();
			if(ExamMainID!=null){
				Element ExamLevel_IDEle=(Element)doc.selectSingleNode("/Root/ExamLevel_ID");
				String ExamLevel_ID=ExamLevel_IDEle.getText();
				Element CommentnotesEle=(Element)doc.selectSingleNode("/Root/Commentnotes");
				String Commentnotes=CommentnotesEle.getText();
				SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String BeginTime=df.format(new Date());
				GUID guid=new GUID();
				String ExamResultGUID=guid.toString();
				String W_ExamResult="W_ExamResult_"+ep_id;
				String sql="insert into "+W_ExamResult+" (ExamResultGUID,ExamLevel_ID,Commentnotes,HANDLEGROUP_ID,HANDLEBY_ID,CREATEDATE,ExamMainID)" +
						"Values(?,?,?,?,?,?,?)";
				Object[] params=new Object[]{ExamResultGUID,ExamLevel_ID,Commentnotes,HandleGroup_ID,Staff_ID,BeginTime,ExamMainID};  
				int[] types=new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.TIMESTAMP,Types.VARCHAR};  
				int sql_result=util.UpdatePersonUseExecute(sql,types,params,dataSource);
				Document result=util.getDoc();
				Element root=result.addElement("R");
				root.addElement("Result").addText("1");
				root.addElement("ResultLEN").addText(sql_result+"");
				ReturnXml=result.asXML();
				return ReturnXml;
			}else{
				throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamMainIDEle.getText()==null");
			}
		}else{
			throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamMainIDEle==null");
		}
	}

}
